<?php
class comment_model_data extends Model {
	public function __construct(){
		parent::__construct();
		$this->tab_res = 'app_com_resources';
		$this->tab_thread = 'app_com_thread';
		$this->tab_post = 'app_com_post';
		
	}
	
	/**
	 * 获取我评论的列表
	 *
	 * @param unknown_type $uid
	 * @return unknown
	 */
	/*public function getMyCommentsList($uid,$page = 1,$pageSize = 16){
		
		if(!$uid) return false;

		$sql = "SELECT thread_id,res_id,content,create_time FROM app_com_post  WHERE author_id = $uid AND type = 1";
		$list =  $this->db->selectPage($sql,array(),$page,$pageSize);
		print_r($list['ROWS']);
		$_rid = $rs = array();
		if($list['ROWS']){
			while ($v = current($list['ROWS'])){
				array_push($_rid,"'".$v['res_id']."'");
				next($list['ROWS']);
			}
			print_r($_rid);
			$_rid = array_unique($_rid);
			
			reset($list['ROWS']);
			$res_list = $this->resourceList($_rid);
			$i = 0;
			while ($row = current($list['ROWS'])){
				$rs[$i] = $row;
				$ctinfo = $res_list[$row['res_id']];
				$rs[$i]['title']	=	$ctinfo['ct_title'];
				$rs[$i]['id']		=	$ctinfo['id'];
				$rs[$i]['cover']	=	$ctinfo['ct_cover'];
				$rs[$i]['dateline']	=	$ctinfo['dateline'];
				$rs[$i]['comment_num'] = $ctinfo['comment_num'];
				$rs[$i]['create_time']	=	$ctinfo['create_time'];		
				next($list['ROWS']);	
			}
			$list['ROWS']	=	$rs;
		}
		return $list;
	}*/
	/**
	 * 获取资源id
	 *
	 * @param unknown_type $resid
	 */
	public function getMyCommentsList($uid,$page=1,$pageSize=10){
//		$resid = is_array($resid)?implode(',',$resid):$resid;
		/*$sql = "SELECT ct.id,ct.ct_title,ct.ct_uid,ct.ct_cover,ct.ct_dateline,cr.res_id,cr.uid,cr.comment_num,create_time FROM cartoon_cartoon ct, app_com_resources cr 
				WHERE ct.id = cr.id AND cr.res_id IN ($resid) ";*/
		if(!$uid) return false;
		$sql = "SELECT  ct.id,ct.ct_title,ct.ct_uid,ct.ct_cover,ct.ct_dateline,cr.res_id,cr.uid,cr.comment_num, cr.create_time ,cp.res_id,cp.content,cp.create_time as comment_time
				FROM cartoon_cartoon ct, app_com_resources cr,
				(SELECT  res_id,content,create_time FROM app_com_post  WHERE author_id =1 AND type = 1 group by res_id) cp
				WHERE ct.id = cr.id AND cr.res_id = cp.res_id  ";
		return $this->db->selectPage($sql,array(),$page,$pageSize);
		
	}
	
	/**
	 * 评论我的。。。。
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $page
	 * @param unknown_type $pageSize
	 */
	public function getCommentList($uid,$page = 1,$pageSize = 16){
		/*if(!$uid) return false;
//		$sql = "SELECT ct.id,ct.ct_title AS title,ct.ct_cover,ct.ct_uid,cm.thread_id,cm.author_id,cm.content,cm.create_time FROM cartoon_cartoon ct,app_com_post cm  WHERE ct.id = cm.res_id AND ct.ct_uid = $uid AND cm.deleted = 0 ORDER BY cm.create_time DESC ";
		$sql = "SELECT ct.id,ct.ct_title AS title,ct.ct_cover,ct.ct_uid,cr.res_id,cr.uid,cr.comment_num FROM cartoon_cartoon ct,app_com_resources cr
				WHERE ct.id = cr.id AND ct.ct_uid = $uid AND cr.deleted = 0";
		$list = $this->db->selectPage($sql,array(),$page,$pageSize);
		$res_id = array();
		while ($v = current($list['ROWS'])) {
			array_push($res_id,'\''.$v['res_id'].'\'');
		}*/
		if(!$uid) return false;
		$sql = "SELECT  ct.id,ct.ct_title,ct.ct_uid,ct.ct_cover,ct.ct_dateline,cr.res_id,cr.uid,cr.comment_num, cr.create_time ,cp.res_id,cp.content,cp.create_time as comment_time,cp.author_id
				FROM cartoon_cartoon ct INNER JOIN app_com_resources cr ON ct.id = cr.id LEFT JOIN app_com_post cp ON cr.res_id = cp.res_id 
				WHERE ct.ct_uid = $uid  AND cp.content !='' AND cp.author_id != $uid GROUP BY ct.id ";
		return $this->db->selectPage($sql,array(),$page,$pageSize);
	}
	
	/**
	 * 获取资源最新$num个评论
	 *
	 * @param unknown_type $res_id
	 * @param unknown_type $num
	 * @return unknown
	 */
	public function getNewComment($res_id,$num = 3){
		if(!$res_id) return false;
		$sql = "SELECT t.thread_id,t.author_id,t.reply_time,t.create_time,p.res_id,p.author_name,p.content FROM $this->tab_thread t LEFT JOIN $this->tab_post p ON t.thread_id = p.thread_id WHERE t.res_id = '".$res_id."' AND p.type = 1  and t.deleted = 0 ORDER BY t.thread_id DESC LIMIT $num ";
		return $this->db->fetchAll($sql);
	}

	public function getCommentContent($res_id,$uid){
		$sql = "select p.* from app_com_post  p left join app_com_thread t on p.thread_id = t.thread_id where t.res_id='".$res_id."' and t.author_id = {$uid} order by p.create_time desc";	
		return $this->db->fetchRow($sql);
		
	}	
	
	//得到一个资源的评论的所有内容
	public function get_comment_data($res_id,$uid=null){
		//单表查询
		$sql = "select * from app_com_thread where `res_id` = '".$res_id."' and deleted = 0  ";
		if($uid) $sql.=" AND author_id = $uid  ";
		$sql.=' order by reply_time desc limit 40 ';
		$data_thread = $this->db->fetchAll($sql);
		$s = '';
		if(count($data_thread) != 0){
			foreach($data_thread as $v){
				$s .= $v['thread_id'].",";
			}
			$s = substr($s,0,-1);
			$sql = "select * from app_com_post where deleted = 0 and thread_id in (" . $s . ")  order by create_time asc ";
			$data_all = $this->db->fetchAll($sql);
			$num  = count($data_all);
			$num2 = count($data_thread);//合并一个资源的内容
			for($i=0;$i<$num;$i++){
				$j = 0;
				for($j=$num2-1;$j>=0;$j--){
					if($data_thread[$j]['thread_id'] == $data_all[$i]['thread_id'])
					$post_data[$data_thread[$j]['thread_id']][] = $data_all[$i];
				}
			}
			if(count($post_data) > 1) $post_data = array_reverse($post_data);
			return $post_data;
		}else{
			return false;
		}

	}
	
	//动态接口
	
	public function getCommentByMid($mid, $id, $uid){

		$res_id = $this->makeResidByModel($mid, $id);

		return $this->get_comment_data($res_id);


	}
	
	//生成动态格式数据
	public function getCommentData($res_id){
		//单表查询
		$sql = "select * from app_com_thread where `res_id` = '".$res_id."' and `deleted` = 0  order by reply_time desc limit 40";
		$data_thread = $this->db->fetchAll($sql);
		$s = '';
		if (count($data_thread) != 0) {
			foreach($data_thread as $v){
				$s .= $v['thread_id'].",";
			}
			$s = substr($s,0,-1);
			$sql = "select * from app_com_post where `deleted` = 0 and  thread_id in (" . $s . ")  order by create_time asc ";
			$data_all = $this->db->fetchAll($sql);
			$num = count($data_all);
			$num2 = count($data_thread);//合并一个资源的内容

			for($i=0;$i<$num;$i++){
				$j = 0;
				//					$data_thread[$i]['content'] = $preg_replace(array("/\n/","/\r/"), array("<br />",""), addslashes($data_thread[$i]['content']));
				for($j=$num2-1;$j>=0;$j--){
					if ($data_thread[$j]['thread_id'] == $data_all[$i]['thread_id']) {
						if ($data_all[$i]['type'] == 1 ) {
							$data_thread[$j] = $data_thread[$j] + $data_all[$i];
						} else {
							$data_thread[$j]['comment'][] = $data_all[$i];
						}
					}
				}
			}
			//				if(count($data_thread) > 1) $data_thread = array_reverse($data_thread);

			return $data_thread;
		}else{
			return false;
		}

	}
	
	
	//生成res_id
	public function makeResidByModel($mid,$id, $uid){
		$arrayMid = array(
		//									1	=> 'avatar',
		//									2	=> 'friends',
		//									3	=> 'setting',
		//									4	=> 'state',
		4	=> array ( 'res_id'  => 'commentsignaturestate_show',
		'app'	 =>	'comment',
		'control'=> 'signature',
		'act'	 => 'state_show',
		),
		5	=>  array ( 'res_id' => 'blogindexview_thread',
		'app'	 =>	'blog',
		'control'=> 'index',
		'act'	 => 'view_thread',
		),
		6	=>  array ( 'res_id' => 'albumphotoview_thread',
		'app'	 =>	'album',
		'control'=> 'photo',
		'act'	 => 'view_thread',
		),
		7	=> array ( 'res_id'  => 'favorite2indexfavorite_show ',
		'app'	 =>	'favorite2',
		'control'=> 'index',
		'act'	 => 'favorite_show',
		),
		8	=> array ( 'res_id'  => 'armoryindexmy_armory',
		'app'	 =>	'armory',
		'control'=> 'index',
		'act'	 => 'my_armory',
		),
		9	=> array ( 'res_id'  => 'pollindexviewpoll',
		'app'	 =>	'poll',
		'control'=> 'index',
		'act'	 => 'viewpoll'
		),
		10	=>  array ( 'res_id' => 'citeindexviewcite',
		'app'	 =>	'cite',
		'control'=> 'index',
		'act'	 => 'viewcite',
		),
		//									11	=> 'sns_comment',
		//									20	=> 'nga',
		//									21	=> 'bbs',
		//									22	=> 'comment'
		);

		$arrayMid[$mid]['res_id'] = md5($arrayMid[$mid]['res_id'].$id);

		return $arrayMid[$mid];
	}
		
		//取得模块id
		public function getModelIdByApp($app) {
				$feedType = array(
							   	 'avatar'	=> 1 ,		//头像
							 	 'friends'	=> 2 ,		//好友
							 	 'setting'	=> 3 ,		//个人信息
							   	 'comment'	=> 4 ,		//心情
							   	 'blog'		=> 5 ,		//日志
							   	 'album'	=> 6 ,		//相册
							 	 'favorite2' => 7 ,		//收藏
							  	 'armory'	=> 8 ,		//魔兽
								 'poll'		=> 9 ,		//投票
								 'cite'		=> 10  ,	//转帖
								// 'rating'	=> 14 ,		// 评比
							 	 'sns_comment' => 11 ,	//SNS站内评论
							   	 'nga'		=>  20 	,	//NGA
							  	 'bb'		=>  21 	,	//BBS

				 );
				return  isset($feedType[$app]) ? $feedType[$app] : 0 ;				
		}
		
		public function regular($text)
		{
//			$text = preg_replace("/\<p\>(.+)\<\/p\>/is", '$1\r\n\r', $text);
//			$text = preg_replace("/\<div\>(.+)\<\/div\>/is", '$1\r\n\r', $text);
//			$text = preg_replace(array('<br _moz_dirty=""/>',"<br>","<p>","&nbsp;"), array("/\n/",'/\n/','/\n/'," "), addslashes($text));
			$repl = array('<br _moz_dirty=""/>' => "\n",'<br>' => "\n");  $text = strtr($text,$repl); 
			$text = strip_tags($text);
			return $text ;
		}

		public function privacy($login_uid, $res, $result)
		{

			//$sql = "select id , username , privacy from prop_standard where id = {$res['uid']}";
			//$result = $this->db->fetchRow($sql);
			$gender = $this->getGender($res['uid']);
			if ($result['privacy']) {				
				$privacy = unserialize($result['privacy']);
				if ($privacy['comments'] == -1) {
					if ( $res['app'] == 'comment' && $res['signature'] == '' && $res['act'] == 'state_show' ) {
						$str = comment_util_tip::$text['signature2'];
					} else {
						$str = comment_util_tip::$text['close'];
					}				
					$str = str_replace('{username}', $gender, $str);
					echo $str; exit;
				} elseif ($privacy['comments'] == 1) {

					$isfriend = friends_util_checker::isFriend($login_uid, $res['uid']);		

					if(!$isfriend)  {
							if ( $res['app'] == 'comment' && $res['signature'] == '' && $res['act'] == 'state_show' ) {
								$str = comment_util_tip::$text['signature1'];
								
							} else {
								$str = comment_util_tip::$text['friend'];
								
							}
							$str = str_replace('{username}', $gender, $str);
							echo $str; exit();
	
					}
				}

			}
		}
		
		public function getGender($uid)
		{
			$gender = index_util_prop::getUserGender($this->friend_uid);
				if($gender=='男'){
					$sex ='他';
				
				}elseif($gender=='女'){
					$sex ='她';
				
				}else{
					$sex = 'TA';
				
				}
				return $sex;
		}
}